import time

from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_security_management_locator import SecurityManagementLocator
from Case.python_ui.Test_data.menu_name_data import MenuName
import re


class SecurityManagement(Basepage):

    # 等待页面加载转动
    def wait_web_page_rotate(self, time=100):
        doc = '等待页面加载转动图标'
        try:
            self.wait_elevisble(SecurityManagementLocator.wait, doc, time)
            self.wait_eleinvisble(SecurityManagementLocator.wait, doc, time)
        except:
            pass

    # 判断插叙列表数量，返回列表数据量
    def page_data_total(self):
        doc = '返回列表查询数据量'
        self.wait_web_page_rotate(5)
        try:
            self.wait_elevisble(SecurityManagementLocator.page_total, doc, 5)
            total = self.get_element(SecurityManagementLocator.page_total, doc).text
            # 使用正则表达式模式对字符串进行拆分
            pattern = r"[ ]"  # 模式表示逗号、感叹号、问号或空格出现一次或多次
            result = re.split(pattern, total)
            return int(result[1])
        except:
            return 0

    def enter_security_audit(self):
        doc = '进入到security_audit页面'
        # js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=100000'
        # self.excute_js(js_object)
        self.scroll_bar_operation(SecurityManagementLocator.security_management_tab)
        time.sleep(2)
        # self.wait_elevisble(SecurityManagementLocator.security_management_tab1,doc)
        # self.click_element(SecurityManagementLocator.security_management_tab1,doc)
        # time.sleep(2)
        self.wait_elevisble(SecurityManagementLocator.security_audit_tab, doc)
        self.click_element(SecurityManagementLocator.security_audit_tab, doc)
        try:
            self.wait_elevisble(SecurityManagementLocator.wait, doc)
            self.wait_eleinvisble(SecurityManagementLocator.wait, doc)
        except:
            pass
        time.sleep(3)

    #

    # 判断某个属性值是否符合预期
    def check_attribute(self, real_result, expect_result):
        if real_result == expect_result:
            assert True
        else:
            assert False, '实际值：' + real_result + '期望值：' + expect_result

    # 检验列表属性字段
    def check_security_audit_list_attribute(self):
        doc = '列表属性检查'
        time.sleep(2)
        self.wait_elevisble(SecurityManagementLocator.audit_list_account_email, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_list_account_status, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_list_account_security_score, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_list_account_user_name, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_list_account_phone_number, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_list_account_organization, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_list_account_online_status, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_list_account_latest_online_time, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_list_account_actions, doc)
        self.wait_elevisble(SecurityManagementLocator.first_account_email, doc)
        first_account_email_one = self.get_element(SecurityManagementLocator.first_account_email, doc).text
        # 接下来判断翻页
        self.wait_elevisble(SecurityManagementLocator.page_total, doc)
        total = self.get_element(SecurityManagementLocator.page_total, doc).text
        # 使用正则表达式模式对字符串进行拆分
        pattern = r"[ ]"  # 模式表示逗号、感叹号、问号或空格出现一次或多次
        result = re.split(pattern, total)
        print(result[1])
        if int(result[1]) > 10:
            self.wait_elevisble(SecurityManagementLocator.page_total_one, doc)
            self.move_mouse_to_click_element(SecurityManagementLocator.page_total_one, doc)
            try:
                self.wait_elevisble(SecurityManagementLocator.wait, doc)
                self.wait_eleinvisble(SecurityManagementLocator.wait, doc)
            except:
                pass
            self.wait_elevisble(SecurityManagementLocator.first_account_email, doc)
            first_account_email_two = self.get_element(SecurityManagementLocator.first_account_email, doc).text
            assert first_account_email_one != first_account_email_two

    # search
    def search_by_account_email(self, account_email):
        doc = '输入account_email查询'
        self.wait_elevisble(SecurityManagementLocator.search_account_email, doc)
        self.send_keys(SecurityManagementLocator.search_account_email, account_email, doc)
        self.wait_elevisble(SecurityManagementLocator.click_saerch, doc)
        self.move_mouse_to_click_element(SecurityManagementLocator.click_saerch, doc)
        self.wait_web_page_rotate()
        time.sleep(2)
        self.wait_elevisble(SecurityManagementLocator.page_total, doc)
        total = self.get_element(SecurityManagementLocator.page_total, doc).text
        # 使用正则表达式模式对字符串进行拆分
        pattern = r"[ ]"  # 模式表示逗号、感叹号、问号或空格出现一次或多次
        result = re.split(pattern, total)
        assert int(result[1]) == 1

    # 根据Security Score查询
    def search_by_security_score(self, str='81-100'):
        doc = "根据security_score区间进行查询，返回查询到的数量"
        self.wait_elevisble(SecurityManagementLocator.click_select_security_score, doc)
        self.move_mouse_to_click_element(SecurityManagementLocator.click_select_security_score, doc)
        self.wait_elevisble(SecurityManagementLocator.get_security_score(self, str), doc)
        self.move_mouse_to_click_element(SecurityManagementLocator.get_security_score(self, str), doc)
        self.move_mouse_to_click_element(SecurityManagementLocator.click_saerch, doc)
        # 页面加载等待
        self.wait_web_page_rotate()
        try:
            self.wait_elevisble(SecurityManagementLocator.page_total, doc, 5)
            total = self.get_element(SecurityManagementLocator.page_total, doc).text
            # 使用正则表达式模式对字符串进行拆分
            pattern = r"[ ]"  # 模式表示逗号、感叹号、问号或空格出现一次或多次
            result = re.split(pattern, total)
            return int(result[1])
        except:
            return 0
        finally:
            self.wait_elevisble(SecurityManagementLocator.click_select_security_score, doc)
            self.move_mouse_to_click_element(SecurityManagementLocator.click_select_security_score, doc)
            self.wait_elevisble(SecurityManagementLocator.get_security_score(self, str), doc)
            self.move_mouse_to_click_element(SecurityManagementLocator.get_security_score(self, str), doc)
            self.move_mouse_to_click_element(SecurityManagementLocator.click_saerch, doc)
            # 页面加载等待
            self.wait_web_page_rotate()

    # 查询列表第一条数据Account Email 来进行查询
    def search_by_account_email_get_first(self):
        doc = "获取查询列表第一条数据的Account Email，进行查询"
        self.wait_elevisble(SecurityManagementLocator.first_account_email, doc)
        first_account_email_one = self.get_element(SecurityManagementLocator.first_account_email, doc).text
        self.search_by_account_email(first_account_email_one)

    # 查询列表第一条数据Phone Number 来进行查询
    def search_by_phone_number_get_first(self):
        doc = "获取查询列表第一条数据的Phone Number，进行查询"
        self.wait_elevisble(SecurityManagementLocator.first_phone_number, doc)
        first_phone_number_one = self.get_element(SecurityManagementLocator.first_phone_number, doc).text
        self.search_by_account_email(first_phone_number_one)

    # 根据Security Score进行查询
    def search_by_security_score_check(self):
        doc = '交验根据security_score区间进行查询'
        try:
            self.wait_elevisble(SecurityManagementLocator.page_total, doc)
            total = self.get_element(SecurityManagementLocator.page_total, doc).text
            # 使用正则表达式模式对字符串进行拆分
            pattern = r"[ ]"  # 模式表示逗号、感叹号、问号或空格出现一次或多次
            result = re.split(pattern, total)
            total1 = int(result[1])
        except:
            pass
        num1 = self.search_by_security_score()
        num2 = self.search_by_security_score('61-80')
        num3 = self.search_by_security_score('41-60')
        num4 = self.search_by_security_score('21-40')
        num5 = self.search_by_security_score('0-20')
        total2 = num1 + num2 + num3 + num4 + num5
        print('total:', total1, total2)
        assert total1 == total2, '期望值：' + str(total1) + '实际值:' + str(total2)

    # 进入log页面，检查log页面元素
    def enter_log_page_and_check(self):
        doc = "进入log页面"
        self.wait_elevisble(SecurityManagementLocator.first_log_button, doc)
        self.move_mouse_to_click_element(SecurityManagementLocator.first_log_button, doc)
        self.wait_web_page_rotate()
        self.wait_elevisble(SecurityManagementLocator.audit_log_list_request_time, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_log_list_request_ip, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_log_list_request_form, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_log_list_risk_operation, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_log_list_mapping_rule, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_log_list_score_reduce, doc)
        self.wait_elevisble(SecurityManagementLocator.audit_log_list_remarks, doc)

    """
       APIGW APP Audit 
    """

    # 进入 APIGW APP Audit
    def enter_apigw_app_audit(self):
        doc = '进入到APIGW APP Audit 页面'
        self.scroll_bar_operation(SecurityManagementLocator.security_management_tab)
        time.sleep(2)
        self.wait_elevisble(SecurityManagementLocator.apigw_app_audit_tab, doc)
        self.click_element(SecurityManagementLocator.apigw_app_audit_tab, doc)

    # Audit页面列表属性检查
    def check_apigw_app_audit_list_attribute(self):
        doc = 'APIGW APP Audit页面列表属性检查'
        page_total = self.page_data_total()
        self.wait_elevisble(SecurityManagementLocator.get_apigw_app_audit_list1(self, 'APP Name'), doc)
        self.wait_elevisble(SecurityManagementLocator.get_apigw_app_audit_list1(self, 'Last Access Time (UTC+08:00)'),
                            doc)
        self.wait_elevisble(SecurityManagementLocator.get_apigw_app_audit_list2(self, 'Status'), doc)
        self.wait_elevisble(SecurityManagementLocator.get_apigw_app_audit_list1(self, 'Last Access IP'), doc)
        self.wait_elevisble(SecurityManagementLocator.get_apigw_app_audit_list1(self, 'Last Inquiry User'), doc)
        self.wait_elevisble(SecurityManagementLocator.get_apigw_app_audit_list1(self, 'APP ID'), doc)
        self.wait_elevisble(SecurityManagementLocator.get_apigw_app_audit_list1(self, 'APP Binding User Email'), doc)
        self.wait_elevisble(SecurityManagementLocator.get_apigw_app_audit_list2(self, 'APIGW User Type'), doc)
        self.wait_elevisble(SecurityManagementLocator.get_apigw_app_audit_list1(self, 'Actions'), doc)
        if page_total != 0:
            assert True
        else:
            assert False, '列表无数据'
            self.save_screen(doc)

    # 根据app_name 去查询
    def search_apigw_app_audit_by_app_name(self):
        doc = 'APIGW APP Audit页面查询条件检查'
        page_total = self.page_data_total()
        if page_total != 0:
            self.wait_elevisble(SecurityManagementLocator.first_app_name, doc)
            first_app_name = self.get_element(SecurityManagementLocator.first_app_name, doc).text
            self.wait_elevisble(SecurityManagementLocator.app_name, doc)
            self.send_keys(SecurityManagementLocator.app_name, first_app_name, doc)
            self.wait_elevisble(SecurityManagementLocator.click_app_audit_search, doc)
            self.click_element(SecurityManagementLocator.click_app_audit_search, doc)
            self.wait_web_page_rotate()
            assert self.page_data_total() == 1
        else:
            assert False, '列表无数据'
            self.save_screen(doc)

    # 根据app_binding_email 去查询
    def search_apigw_app_audit_by_app_binding_email(self):
        doc = 'APIGW APP Audit页面查询条件检查'
        page_total = self.page_data_total()
        if page_total != 0:
            self.wait_elevisble(SecurityManagementLocator.first_app_binding_email, doc)
            first_app_binding_email = self.get_element(SecurityManagementLocator.first_app_binding_email, doc).text
            self.wait_elevisble(SecurityManagementLocator.app_binding_email, doc)
            self.send_keys(SecurityManagementLocator.app_binding_email, first_app_binding_email, doc)
            self.wait_elevisble(SecurityManagementLocator.first_last_inquiry_user_email, doc)
            first_last_inquiry_user_email = self.get_element(SecurityManagementLocator.first_last_inquiry_user_email,
                                                             doc).text
            self.wait_elevisble(SecurityManagementLocator.last_inquiry_user_email, doc)
            self.send_keys(SecurityManagementLocator.last_inquiry_user_email, first_last_inquiry_user_email, doc)
            self.wait_elevisble(SecurityManagementLocator.click_app_audit_search, doc)
            self.click_element(SecurityManagementLocator.click_app_audit_search, doc)
            self.wait_web_page_rotate()
            assert self.page_data_total() == 1
        else:
            assert False, '列表无数据'
            self.save_screen(doc)

    # 进入log页面，检查log页面
    def enter_apigw_app_audit_log(self):
        doc = '进入APIGW APP Audit的log页面'
        self.wait_web_page_rotate()
        page_total = self.page_data_total()
        if page_total != 0:
            self.wait_elevisble(SecurityManagementLocator.first_app_name, doc)
            first_app_name = self.get_element(SecurityManagementLocator.first_app_name, doc).text
            self.wait_elevisble(SecurityManagementLocator.first_click_log, doc)
            self.move_mouse_to_click_element(SecurityManagementLocator.first_click_log, doc)
            self.wait_web_page_rotate()
            self.wait_elevisble(SecurityManagementLocator.apigw_app_audit_log_page, doc)
            log_page_title = self.get_element(SecurityManagementLocator.apigw_app_audit_log_page, doc).text
            first_app_name = 'Log of ' + first_app_name
            text = '预期值:' + first_app_name + '实际值:' + first_app_name
            assert first_app_name == log_page_title, text
        else:
            assert False, '列表无数据'
            self.save_screen(doc)

        # 进入log页面，检查log页面

    def enter_apigw_app_audit_log2(self):
        doc = '进入APIGW APP Audit的log页面,检查查询条件'
        self.wait_web_page_rotate()
        page_total = self.page_data_total()
        if page_total != 0:
            self.wait_elevisble(SecurityManagementLocator.first_click_log, doc)
            self.move_mouse_to_click_element(SecurityManagementLocator.first_click_log, doc)
            self.wait_web_page_rotate()
            self.wait_elevisble(SecurityManagementLocator.request_path, doc)
            self.wait_elevisble(SecurityManagementLocator.inquiryUser, doc)
            self.wait_elevisble(SecurityManagementLocator.requestId, doc)
            self.wait_elevisble(SecurityManagementLocator.date, doc)
            assert True
        else:
            assert False, '列表无数据'
            self.save_screen(doc)

    """
       Product Security Rule 
    """

    # 进入 Product Security Rule
    def enter_product_security_rule(self):
        doc = '进入到Product Security Rule 页面'
        self.scroll_bar_operation(SecurityManagementLocator.security_management_tab)
        time.sleep(2)
        self.wait_elevisble(SecurityManagementLocator.product_security_rule_tab, doc)
        self.click_element(SecurityManagementLocator.product_security_rule_tab, doc)

    # 判断进入Product Security Rule页面
    def enter_product_security_rule_sucess(self):
        doc = '判断进入到Product Security Rule 页面'
        self.enter_product_security_rule()
        self.wait_web_page_rotate()
        self.wait_elevisble(SecurityManagementLocator.elastic_compute_service_menu, doc)
        self.move_mouse_to_click_element(SecurityManagementLocator.elastic_compute_service_menu, doc)
        time.sleep(2)
        self.wait_elevisble(SecurityManagementLocator.check_elastic_compute_service_title, doc)
        assert True

    def add_product_security_rule_forbidden_data(self, cmdopt):
        doc = '添加拦截地址'
        self.enter_product_security_rule()
        page_total = self.page_data_total()
        self.wait_elevisble(SecurityManagementLocator.click_import, doc)
        self.move_mouse_to_click_element(SecurityManagementLocator.click_import, doc)
        self.wait_elevisble(SecurityManagementLocator.forbidden_date_list, doc)
        self.send_keys(SecurityManagementLocator.forbidden_date_list, MenuName.forbidden_data[cmdopt], doc)
        self.wait_elevisble(SecurityManagementLocator.click_submit, doc)
        self.move_mouse_to_click_element(SecurityManagementLocator.click_submit, doc)
        self.wait_web_page_rotate()
        page_total_new = self.page_data_total()
        print(page_total, page_total_new)
        assert page_total_new == page_total + 1

    def delete_product_security_rule_forbidden_data(self, cmdopt):
        doc = '删除添加的拦截地址'
        time.sleep(2)
        page_total = self.page_data_total()
        self.wait_elevisble(SecurityManagementLocator.click_delete(self, MenuName.forbidden_data[cmdopt]), doc)
        self.move_mouse_to_click_element(SecurityManagementLocator.click_delete(self, MenuName.forbidden_data[cmdopt]),
                                         doc)
        time.sleep(2)
        self.wait_elevisble(SecurityManagementLocator.click_Confirm, doc)
        self.move_mouse_to_click_element(SecurityManagementLocator.click_Confirm, doc)
        self.wait_web_page_rotate(5)
        page_total_new = self.page_data_total()
        assert page_total_new == page_total - 1

    def open_security_inspect_on(self):
        doc = '开启拦截'
        self.wait_elevisble(SecurityManagementLocator.security_inspect_on, doc)
        # 用于判断开关是否开启
        aria_checked = self.get_attribute(SecurityManagementLocator.security_inspect_on, 'aria-checked', doc)
        if aria_checked == 'false':
            self.move_mouse_to_click_element(SecurityManagementLocator.security_inspect_on, doc)
            time.sleep(2)
            self.wait_elevisble(SecurityManagementLocator.click_i_acknowledge, doc)
            self.move_mouse_to_click_element(SecurityManagementLocator.click_i_acknowledge, doc)
            self.wait_elevisble(SecurityManagementLocator.click_Confirm, doc)
            self.move_mouse_to_click_element(SecurityManagementLocator.click_Confirm, doc)
            time.sleep(2)
            # 用于判断开关是否开启
            aria_checked = self.get_attribute(SecurityManagementLocator.security_inspect_on, 'aria-checked', doc)
            assert aria_checked == 'true'

    def close_security_inspect_on(self):
        doc = '关闭拦截'
        self.wait_elevisble(SecurityManagementLocator.security_inspect_on, doc)
        # 用于判断开关是否开启
        aria_checked = self.get_attribute(SecurityManagementLocator.security_inspect_on, 'aria-checked', doc)
        if aria_checked == 'true':
            self.move_mouse_to_click_element(SecurityManagementLocator.security_inspect_on, doc)
            time.sleep(2)
            self.wait_elevisble(SecurityManagementLocator.click_i_acknowledge, doc)
            self.move_mouse_to_click_element(SecurityManagementLocator.click_i_acknowledge, doc)
            self.wait_elevisble(SecurityManagementLocator.click_Confirm, doc)
            self.move_mouse_to_click_element(SecurityManagementLocator.click_Confirm, doc)
            time.sleep(2)
            # 用于判断开关是否开启
            aria_checked = self.get_attribute(SecurityManagementLocator.security_inspect_on, 'aria-checked', doc)
            assert aria_checked == 'false'
